using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Core.Mapping;


namespace WorldEditor
{
    public partial class WorldZonesGrid : UserControl
    {
        /// <summary>Get/Set Tiled World</summary>
        public TileWorld World { get { return _World; } set { _World = value; } }

        public event EventHandler ZoneClicked;

        protected TileWorld _World;
        
        protected WorldZoneGridItem[,] GridItems;

        public WorldZonesGrid(TileWorld world)
        {
            _World = world;
            //Set the width and height of our world control to the size of 
            // the width/height of the GridItem object times the number of items in the grid for 
            // width/height
            this.Width = 100 * world.ZoneGridWidth;
            this.Height = 100 * world.ZoneGridHeight;

            InitializeComponent();

            CreateZoneItems();
        }

        protected void CreateZoneItems()
        {
            //New Grid Items Array
            GridItems = new WorldZoneGridItem[_World.ZoneGridWidth, _World.ZoneGridHeight];
            //Clear out any other controls from this control
            this.Controls.Clear();

            for (int x = 0; x < _World.ZoneGridHeight; x++)
            {
                for (int y = 0; y < _World.ZoneGridWidth; y++)
                {
                    TileZone tz;

                    if (_World.Zones[x + y * _World.ZoneGridWidth] != null)
                        tz = _World.Zones[x + y * _World.ZoneGridWidth];
                    else
                    {
                        tz = TileZone.EmptyZone(_World);
                        tz.WorldCoordinate = new Point(x * (128 * 5), y * (128 * 5));
                    }

                    WorldZoneGridItem wzgi = new WorldZoneGridItem(tz, new System.Drawing.Point(x,y));
                    wzgi.Clicked += new EventHandler(wzgi_Clicked);
                    wzgi.CreateZone += new EventHandler(wzgi_CreateZone);
                    wzgi.Location = new System.Drawing.Point(x * 100, y * 100);

                    GridItems[x, y] = wzgi;
                    //Add the control to the collection
                    this.Controls.Add(wzgi);
                }
            }
        }

        protected void wzgi_CreateZone(object sender, EventArgs e)
        {
            System.Drawing.Point gpD = (System.Drawing.Point)sender;
            Point gp = new Point(gpD.X, gpD.Y);
            //create a new zone full of empty maps, not null maps
            _World.Zones[gp.X + gp.Y * _World.ZoneGridWidth] = TileZone.NewFullZone(gp, "New Zone", _World);
            //_World.Zones[gp.X + gp.Y * _World.ZoneGridWidth] = new TileZone("New Zone", new Point(gp.X * (128 * 5), gp.Y * (128 * 5)), _World);

            //Instead of recreating the entire set of items we should just refresh the existing one that was changed.
            CreateZoneItems();
        }

        public void wzgi_Clicked(object sender, EventArgs e)
        {
            //Pass along event
            if (ZoneClicked != null)
                ZoneClicked.Invoke(sender, e);
        }
    }
}
